package xin.nick.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import xin.nick.entity.Task;
import xin.nick.mapper.TaskMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import xin.nick.domain.dto.TaskUpdateDTO;
import xin.nick.domain.dto.TaskCreateDTO;
import xin.nick.domain.query.TaskQuery;
import xin.nick.domain.vo.TaskVO;
import xin.nick.util.QueryUtil;
import xin.nick.util.ResultUtil;

import java.util.List;

/**
 * <p>
 * 任务 服务实现类
 * </p>
 *
 * @author Nick
 * @since 2023-06-22
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class TaskService extends ServiceImpl<TaskMapper, Task> {


    /**
     * 获取任务详情
     * @param id Long
     * @return TaskVO
     */
    public TaskVO getDetail(Long id) {
        Assert.notNull(id, "[id]不可为空");
        Task task = getById(id);
        Assert.notNull(task, "任务不存在");
        TaskVO taskVO = BeanUtil.copyProperties(task, TaskVO.class);
        return taskVO;
    }

    /**
     * 获取任务列表
     * @param query TaskQuery
     * @return List<TaskVO>
     */
    public List<TaskVO> getList(TaskQuery query) {

        Task task = BeanUtil.copyProperties(query, Task.class);
        LambdaQueryWrapper<Task> queryWrapper = new LambdaQueryWrapper<>(task);

        List<Task> taskList = list(queryWrapper);

        return BeanUtil.copyToList(taskList, TaskVO.class);

    }

    /**
     * 获取任务列表分页
     * @param query TaskQuery
     * @return Page<TaskVO>
     */
    public Page<TaskVO> getListPage(TaskQuery query) {

        Task task = BeanUtil.copyProperties(query, Task.class);
        LambdaQueryWrapper<Task> queryWrapper = new LambdaQueryWrapper<>(task);

        Page<Task> page = page(QueryUtil.getQueryPage(), queryWrapper);

        return ResultUtil.convertPage(page, TaskVO.class);

    }

    /**
     * 任务添加
     * @param dto TaskCreateDTO
     * @return TaskVO
     */
    public TaskVO createTask(TaskCreateDTO dto) {
        Task task = BeanUtil.copyProperties(dto, Task.class);
        task.setTaskId(null);
        save(task);
        return getDetail(task.getTaskId());
    }

    /**
     * 任务更新
     * @param dto TaskUpdateDTO
     * @return TaskVO
     */
    public TaskVO updateTask(TaskUpdateDTO dto) {
        Assert.notNull(dto.getTaskId(), "[taskId]不可为空");
        Task task = BeanUtil.copyProperties(dto, Task.class);
        updateById(task);
        return getDetail(task.getTaskId());
    }


    /**
     * 任务删除
     * @param id Long
     */
    public void deleteById(Long id) {
        Assert.notNull(id, "[id]不可为空");
        removeById(id);
    }


    /**
     * 任务根据id批量删除
     * @param idList
     */
    public void deleteByIdList(List<Long> idList) {
        if (CollUtil.isEmpty(idList)) {
            return;
        }
        removeByIds(idList);
    }


}
